package com.cdc.code;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.cdc.taskhandler.R;
import com.cdc.toolbars.BaseActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class CodeActivity extends AppCompatActivity{

    private TabLayout tabLayout;
    private Toolbar toolBar;
    private ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_code);
        tabLayout=findViewById(R.id.tabLayout);
        toolBar=findViewById(R.id.toolBar);
        viewPager=findViewById(R.id.viewPager);
        setBackArrow();
       getDate();
        initDate();

    }
    private  Map<String,String> codeMap=new HashMap<>();

    public static final String EXTRA_KEY="extra_key";

    private void getDate(){
        Bundle bundle=this.getIntent().getExtras();
        codeMap= (Map<String, String>) bundle.get(EXTRA_KEY);
    }

    /**
     * 设置左上角back按钮
     */
    public void setBackArrow() {
        setSupportActionBar(toolBar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        final Drawable upArrow = getResources().getDrawable(R.drawable.ic_arrow_back_black_36dp);
        //给ToolBar设置左侧的图标
        getSupportActionBar().setHomeAsUpIndicator(upArrow);
        // 给左上角图标的左边加上一个返回的图标 。对应ActionBar.DISPLAY_HOME_AS_UP
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        //设置返回按钮的点击事件
        toolBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }



    private void initDate(){
        int size=codeMap.size();
        List<Fragment> fragmentList=new ArrayList<>(size);
        Fragment fragment=new Fragment();
        List tabTitleList=new ArrayList();
        for (Map.Entry<String, String> stringStringEntry : codeMap.entrySet()) {
            TabLayout.Tab tab = tabLayout.newTab();
            tabLayout.addTab(tab);
            fragmentList.add(CodeFragment.newInstance(stringStringEntry.getValue()));
            tabTitleList.add(stringStringEntry.getKey());

        }
        ViewPagerAdapter viewPagerAdapter=new ViewPagerAdapter(getSupportFragmentManager(), this, fragmentList, tabTitleList);
        viewPager.setAdapter(viewPagerAdapter);
        tabLayout.setupWithViewPager(viewPager);

    }
}

class ViewPagerAdapter  extends FragmentStatePagerAdapter{

    private List tabTitleList;
    private List fragmentList;


    public ViewPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    public ViewPagerAdapter(FragmentManager fm, Context context,
                            List fragmentList, List tabTitleList) {
        super(fm);
        this.tabTitleList = tabTitleList;
        this.fragmentList = fragmentList;
    }

    @Override
    public Fragment getItem(int position) {
        return (Fragment) fragmentList.get(position);
    }

    @Override
    public int getCount() {
        return tabTitleList.size();
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return tabTitleList.get(position).toString();
    }
}


